package com.booklove.controller.order;

import com.booklove.context.BaseContext;
import com.booklove.domain.dto.NotesFormDTO;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.Notesjudge;
import com.booklove.domain.query.NotesQuery;
import com.booklove.domain.vo.NotesVO;
import com.booklove.result.Result;
import com.booklove.service.order.NotesService;
import com.booklove.domain.po.Notes;
import com.booklove.service.order.NotesjudgeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@Slf4j
@RestController("notesCLL")
@RequestMapping("/notes")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class NotesController {

    private final NotesService notesService;
    private final NotesjudgeService notesjudgeService;

    /**
     * 保存新增
     */
    @PostMapping("/")
    Result<Notes> saveNotes(@RequestBody NotesFormDTO notesFormDTO) {
        return null;
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/id")
    Result<String> deleteNotes(@RequestParam("id") Long id) {
        notesService.removeById(id);
        return Result.success();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    NotesVO getNotes(@PathVariable Long id) {
        NotesVO notesVO = new NotesVO();
        Notes notes = notesService.getById(id);
        BeanUtils.copyProperties(notes, notesVO);

        return notesVO;
    }


    /**
     * 编辑数据
     *
     * @param notesFormDTO 实体
     * @return 编辑结果
     */
    @PutMapping("/")
    Result<String> updateNotes(@RequestBody NotesFormDTO notesFormDTO) {
        Notes notes = new Notes();
        BeanUtils.copyProperties(notesFormDTO, notes);
        notes.setUpdatedTime(LocalDateTime.now());
        notes.setUpdatedBy(BaseContext.getCurrentId().intValue());
        notesService.updateById(notes);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param notesQuery 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    PageDTO<NotesVO> notessPageQuery(NotesQuery notesQuery) {
        notesQuery.setStatus(1);
        return notesService.queryNotessPage(notesQuery);
    }

}

